package me.exchange.com.modules.system.service.impl;

import me.exchange.com.modules.system.domain.FileContent;
import me.exchange.com.utils.*;
import me.exchange.com.modules.system.repository.FileContentRepository;
import me.exchange.com.modules.system.service.FileContentService;
import me.exchange.com.modules.system.dto.FileContentDto;
import me.exchange.com.modules.system.dto.FileContentQueryCriteria;
import me.exchange.com.modules.system.mapper.FileContentMapper;
import me.exchange.com.modules.system.repository.UserRepository;
import me.exchange.com.modules.system.domain.User;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.sql.Timestamp;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
/**
* @author zjj
* @date 2020-05-14
*/
@Service
//@CacheConfig(cacheNames = "fileContent")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class FileContentServiceImpl implements FileContentService {

    private final FileContentRepository fileContentRepository;

    private final FileContentMapper fileContentMapper;

    private final UserRepository userRepository;

    public FileContentServiceImpl(UserRepository userRepository,FileContentRepository fileContentRepository, FileContentMapper fileContentMapper) {
        this.userRepository = userRepository;
        this.fileContentRepository = fileContentRepository;
        this.fileContentMapper = fileContentMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(FileContentQueryCriteria criteria, Pageable pageable){
        Page<FileContent> page = fileContentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(fileContentMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<FileContentDto> queryAll(FileContentQueryCriteria criteria){
        Sort sort = criteria.getSort();
        return fileContentMapper.toDto(fileContentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),sort));
    }

    @Override
    //@Cacheable(key = "#p0")
    public FileContentDto findById(Long id) {
        FileContent fileContent = fileContentRepository.findById(id).orElseGet(FileContent::new);
        ValidationUtil.isNull(fileContent.getId(),"FileContent","id",id);
        return fileContentMapper.toDto(fileContent);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public FileContentDto create(FileContent resources) {
        User user=userRepository.findByUsername(SecurityUtils.getUsername());

        return fileContentMapper.toDto(fileContentRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(FileContent resources) {
        FileContent fileContent = fileContentRepository.findById(resources.getId()).orElseGet(FileContent::new);
        User user=userRepository.findByUsername(SecurityUtils.getUsername());
        ValidationUtil.isNull( fileContent.getId(),"FileContent","id",resources.getId());

        resources.setUAdminTime(new Timestamp(System.currentTimeMillis()));
        fileContentRepository.save(fileContent);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void delete(Long id) {
        FileContent fileContent = fileContentRepository.findById(id).orElseGet(FileContent::new);
        fileContentRepository.save(fileContent);
    }
    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Long[] ids) {
        User user=userRepository.findByUsername(SecurityUtils.getUsername());
        for (Long id : ids) {
            FileContent fileContent = fileContentRepository.findById(id).orElseGet(FileContent::new);
            fileContent.setUAdminTime(new Timestamp(System.currentTimeMillis()));
            fileContentRepository.save(fileContent);
            //fileContentRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<FileContentDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (FileContentDto fileContent : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("Bucket 识别符", fileContent.getBucket());
            map.put("文件名称", fileContent.getName());
            map.put("原文件名", fileContent.getRealName());
            map.put("文件大小", fileContent.getSize());
            map.put("文件类型：私有或公开", fileContent.getType());
            map.put("上传或同步的时间", fileContent.getUpdateTime());
            map.put("文件url", fileContent.getUrl());
            map.put(" suffix",  fileContent.getSuffix());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}